//index.js
//获取应用实例
const app = getApp()
var zUrl = getApp().globalData.zUrl;
Page({
  data: {
    token:'',
    order_list:'',
    navData: [
      {
        text: '全部订单',
      },
      {
        text: '已认购',
      },
      {
        text: '已结余款',
      },
      {
        text: '已放款',
      },
      {
        text: '已结佣',
      },
      {
        text: '退房/退款',
      }


    ],
    currentTab: 0,
    navScrollLeft: 0
  },
  //事件处理函数
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })

    
  },
  
  switchNav(event) {
    this.data.navData.status
    var cur = event.currentTarget.dataset.current;
    //每个tab选项宽度占1/5
    var singleNavWidth = this.data.windowWidth / 5;
    //tab选项居中                            
    this.setData({
      navScrollLeft: (cur - 2) * singleNavWidth
    })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }

  },
  switchTab(event) {
    var cur = event.detail.current;
    var singleNavWidth = this.data.windowWidth / 5;
    this.setData({
      currentTab: cur,
      navScrollLeft: (cur - 2) * singleNavWidth
    });
  },
  onShow(options){
    var that = this
    wx.request({
      url: zUrl+'/api/order/list',
      method: 'POST',
      header: {
        'Cookie': 'laravel_session=' + wx.getStorageSync('laravel_session'),
        Accept: 'application/json',
        Authorization: 'Bearer ' + wx.getStorageSync('token')
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          that.setData({
            order_list: res.data.data
          })
        }
        //console.log(res.data)
      },
      fail: function (error) {

      },
      complete: function (res) {

      }
    })
  },
  onPullDownRefresh: function () {
    var that = this
    wx.request({
      url: zUrl + '/api/order/list',
      data: {},
      method: 'POST',
      header: {
        'Cookie': 'laravel_session=' + wx.getStorageSync('laravel_session'),
        Accept: 'application/json',
        Authorization: 'Bearer ' + wx.getStorageSync('token')
      },
      success: function (res) { 
        wx.hideLoading();
        if (res.data.code == 200) {
          that.setData({
            order_list: res.data.data
          })
        }
      },
      fail: function (res) { },
      complete: function (res) {
        wx.stopPullDownRefresh();
      }
    })
  }
})